/**
 * Created by cyf on 15-12-1.
 * 好友树构建器
 */
(function() {
	qxchat.PersonalBandTree = PersonalBandTree;
	var EXTEND = null; //是否需要继承
	function PersonalBandTree(treeViewId, zNodes) {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		// 初始化菜单
		this.init();
		this.zTreeObj = null;
		this.personalBandTreeRMenu = $("#personalBandTreeRMenu");
		this.currentNode = null;
		this.createBandList(zNodes);
		var self = this;
		var setting = {
			edit: {
				enable: true,
				showRenameBtn: false,
				showRemoveBtn: false
			},
			drag: {
				prev: true,
				next: true,
				inner: false
			},
			view: {
				showLine: false,
				showIcon: false,
				dblClickExpand: true,
				addDiyDom: addDiyDom
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: onClick,
				beforeRename: beforeRename,
				onRightClick: OnRightClick
			}
		};
		//点击事件
		function onClick(event, treeId, treeNode, clickFlag) {
			//是否可点击，可理解为是否为用户节点，如果是打开窗口
			if (!(treeNode.click == false)) {
				global.chartWinHandler.open(treeNode.loginName, treeNode.name)
			} else {
				//self.zTreeObj.expandNode(treeNode)
			}
		}
		//右键事件
		function OnRightClick(event, treeId, treeNode) {
			self.currentNode = treeNode;
			if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
				self.zTreeObj.cancelSelectedNode();
				showPersonalBandTreeRMenu("root", event.clientX, event.clientY);
			} else if (treeNode && !treeNode.noR) {
				self.zTreeObj.selectNode(treeNode);
				showPersonalBandTreeRMenu("node", event.clientX, event.clientY);
			}
		}
		//显示右键菜单
		function showPersonalBandTreeRMenu(type, x, y) {
			if (type == "root") {
				$("#personalBandTree_rename").hide();
				$("#personalBandTree_del").hide();
				$("#personalBandTree_delFirend").hide();
			} else {
				if (self.currentNode.isParent) {
					$("#personalBandTree_rename").show();
					$("#personalBandTree_del").show();
					$("#personalBandTree_delFirend").hide();
				} else {
					$("#personalBandTree_rename").hide();
					$("#personalBandTree_del").hide();
					$("#personalBandTree_delFirend").show();
				}
			}
			$("#personalBandTreeRMenu ul").show();
			var width = self.personalBandTreeRMenu.width();
			var height = self.personalBandTreeRMenu.height();
			if ((x + width) > document.body.offsetWidth) {
				x -= (x + width) - document.body.offsetWidth + 10;
			}
			if ((y + height) > document.body.offsetHeight) {
				y -= (y + height) - document.body.offsetHeight + 10;
			}
			self.personalBandTreeRMenu.css({
				"top": y + "px",
				"left": x + "px",
				"visibility": "visible"
			});
			$("body").bind("mousedown", onBodyMouseDown);
		}
		//点击其它位置隐藏菜单
		function onBodyMouseDown(event) {
			if (!(event.target.id == "personalBandTreeRMenu" || $(event.target).parents("#personalBandTreeRMenu").length > 0)) {
				self.personalBandTreeRMenu.css({
					"visibility": "hidden"
				});
			}
		}
		//添加分组
		function addTreeNode() {
			hidePersonalBandTreeRMenu();
			var newNode = {
				name: "新分组",
				click: false,
				isParent: true
			};
			self.currentNode = self.zTreeObj.addNodes(null, newNode)[0];
			self.zTreeObj.editName(self.currentNode);
		}
		$("#personalBandTree_add").bind("click", addTreeNode);
		//将好友移出分组
		function delFirend() {
			hidePersonalBandTreeRMenu();
			var msg = "确认要将好友移出分组？\n\n请确认！";
			if (confirm(msg) == true) {
				self.zTreeObj.removeNode(self.currentNode);
				var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","bandId":"' + self.currentNode.pId + '","memberLoginName":"' + self.currentNode.loginName + '","type":"BandDelMember",},"type":"event"}';
				global.webSocketHandler.send(message);
			}
		}
		$("#personalBandTree_delFirend").bind("click", delFirend);
		//编辑分组
		function renameTreeNode() {
			hidePersonalBandTreeRMenu();
			self.zTreeObj.editName(self.currentNode);
		}
		$("#personalBandTree_rename").bind("click", renameTreeNode);

		function beforeRename(treeId, treeNode, newName) {
			if (newName.length == 0) {
				alert("节点名称不能为空.");
				setTimeout(function() {
					self.zTreeObj.editName(treeNode)
				}, 10);
				return false;
			}
			var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","bandId":"' + treeNode.id + '","bandName":"' + newName + '","type":"BandEdit",},"type":"event"}';
			global.webSocketHandler.send(message);
			return true;
		}

		//删除分组
		function removeTreeNode() {
			hidePersonalBandTreeRMenu();
			var msg = "确认要删除分组吗？\n\n请确认！";
			if (confirm(msg) == true) {
				self.zTreeObj.removeNode(self.currentNode);
				var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","bandId":"' + self.currentNode.id + '","type":"BandDel",},"type":"event"}';
				global.webSocketHandler.send(message);
			}
		}
		$("#personalBandTree_del").bind("click", removeTreeNode);
		//隐藏菜单
		function hidePersonalBandTreeRMenu() {
			if (self.personalBandTreeRMenu) self.personalBandTreeRMenu.css({
				"visibility": "hidden"
			});
			$("body").unbind("mousedown", onBodyMouseDown);
		}
		//初始化联系人样式
		function addDiyDom(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch")
			var icoObj = $("#" + treeNode.tId + "_ico");
			var text = $("#" + treeNode.tId + "_span");
			var spaceStr = "<span id='" + treeNode.tId + "_span_status' style='padding-left:15px'></span>";
			text.append(spaceStr);
			var ustatus = $("#" + treeNode.tId + "_span_status");
			switchObj.remove();
			icoObj.before(switchObj);

			if (treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
				switchObj.before(spaceStr);
			}
			switch (treeNode.status) {
				case 0:
					ustatus.css("color", "gray");
					ustatus.html("离线");
					break;
				case 1:
					ustatus.css("color", "green");
					ustatus.html("在线");
					break;
			}
		}
		var treeObj = $("#" + treeViewId);
		//构建树
		$.fn.zTree.init(treeObj, setting, zNodes);
		treeObj.addClass("showIcon");
		//当前zTree对象
		this.zTreeObj = $.fn.zTree.getZTreeObj(treeViewId);
		this.zTreeObj.expandAll(true);
		this.zTreeObj.expandAll(false);
	}
	PersonalBandTree.prototype = {
		createBandList: function(zNodes) {
			for (var i = 0; i < zNodes.length; i++) {
				var band = zNodes[i];
				$("#recentContactsTreeRMenu ul").append('<li id="' + band.id + '">' + band.name + '</li>');
				$("#orgTreeRMenu ul").append('<li id="' + band.id + '">' + band.name + '</li>');
			}
			$("#orgTreeRMenu li").bind("click", global.orgTreeObj.firednAddToBand);
			$("#recentContactsTreeRMenu li").bind("click", global.recentContactsDataTreeObj.firednAddToBand);
		},
		init: function() {
			$(document.body).append('<div id="personalBandTreeRMenu" class="rMenu">' + '<ul>' + '	<li id="personalBandTree_add">新建分组</li>' + '	<li id="personalBandTree_rename">重命名分组</li>' + '	<li id="personalBandTree_del">删除分组</li>' + '	<li id="personalBandTree_delFirend">移出分组</li>' + '</ul>' + '</div>');
		}
	}
})();